
;File         WINDOW.ASM

;An assembler for an interactive screen designing utility
;These routines may be called from a C program
;
;     To generate .OBJ file from WINDOW.ASM
;             MASM WINDOW;

public        stkptr,scrstack,scrdata
public        _pushscr
public        _popscr
public        _printv
public        _printw
public        _getstr
public        _box
public        _cls
public        _hidecur
public        _showcur
public        _savecur
public        _restcur


_data         segment  word public 'DATA'
oldcur        dw ?
curpos        dw ?
stkptr        dw offset scrstack
scrstack      db 16000 dup (?)
scrdata       dw 4000 dup (?)
_data         ends

dgroup        group _data

_text         segment  byte public 'CODE'
              assume cs:_text,ds:dgroup

cursor        macro          ;Positions cursor at row/col
              push ax
              push bx
              mov ah,02h
              mov bh,00h
              int 10h
              pop bx
              pop ax
              endm

putchar       macro          ;Writes character & attribute
              push ax        ;Entry:
              push bx        ;      CX = attribute/charater
                             ;      DX = row/column
              xor ah,ah      ;      ES = segment address of
              mov al,dh      ;           video memory
              xor bh,bh
              mov bl,dh
              push cx
              mov cl,7
              shl ax,cl
              mov cl,5
              shl bx,cl
              pop cx
              add ax,bx
              xor bh,bh
              mov bl,dl
              shl bx,1
              add bx,ax
              mov word ptr es:[bx],cx

              pop bx
              pop ax
              endm

putattr       macro          ;Writes attribute only directly
              push ax        ;Entry:
              push bx        ;      CX = attribute/charater
                             ;      DX = row/column
              xor ah,ah      ;      ES = segment address of
              mov al,dh      ;           video memory
              xor bh,bh
              mov bl,dh
              push cx
              mov cl,7
              shl ax,cl
              mov cl,5
              shl bx,cl
              pop cx
              add ax,bx
              xor bh,bh
              mov bl,dl
              shl bx,1
              add bx,ax
              mov byte ptr es:[bx+1],ch

              pop bx
              pop ax
              endm

getchar       macro          ;Gets character & attribute
              push ax        ;Entry:
              push bx        ;      DX = row/column
                             ;      ES = segment address of
              xor ah,ah      ;           video memory
              mov al,dh      ;Returns:
              xor bh,bh      ;      CX = attribute/character
              mov bl,dh
              push cx
              mov cl,7
              shl ax,cl
              mov cl,5
              shl bx,cl
              pop cx
              add ax,bx
              xor bh,bh
              mov bl,dl
              shl bx,1
              add bx,ax
              mov cx,word ptr es:[bx]

              pop bx
              pop ax
              endm

cls           macro ulr,ulc,lrr,lrc
              push ax
              push bx
              push cx
              push dx
              mov ah,6
              xor al,al
              mov ch,ulr
              mov cl,ulc
              mov dh,lrr
              mov dl,lrc
              mov bh,0fh
              int 10h
              pop dx
              pop cx
              pop bx
              pop ax
              endm

frame         equ    [bp]
INITATTR      equ    0fh

_pushscr      proc near          ;push current video memory
              push si
              push di
              push es
              push ds

              xor ax,ax
              mov es,ax
              cmp byte ptr es:[449h],7
              je a01
              mov ax,0b800h
              jmp a02
a01:          mov ax,0b000h
a02:          push ds
              pop es

              xor si,si
              mov di,stkptr
              mov cx,4000
              cld
              mov ds,ax
              rep movsb

              pop ds
              pop es
              pop di
              pop si

              add stkptr,4000
              ret
_pushscr      endp

_popscr       proc near          ;pop current video memory
              push si
              push si
              push es

              xor ax,ax
              mov es,ax
              cmp byte ptr es:[449h],7
              je b01
              mov ax,0b800h
              jmp b02
b01:          mov ax,0b000h
b02:          mov es,ax
              cmp stkptr,offset ds:scrstack
              jbe b03
              sub stkptr,4000
b03:          mov si,stkptr
              xor di,di
              mov cx,4000
              cld
              rep movsb

              pop es
              pop di
              pop si
              ret
_popscr       endp

_printv       proc near          ;void printv (str,att,row,col);
                                 ;char *str;
                                 ;int att,row,col;

aframe        struc
abptr         dw     ?
aret_ad       dw     ?
str_ad        dw     ?
att           dw     ?
row           dw     ?
col           dw     ?
aframe        ends

              push bp
              mov bp,sp
              push di
              push si
              mov si,frame.str_ad     ;DS:SI point to start of source string
              xor ax,ax
              mov es,ax
              cmp byte ptr es:[449h],7 ;test video mode
              je c01
              mov ax,0b800h           ;color buffer
              jmp c02
c01:          mov ax,0b000h           ;monochrome buffer
c02:          mov es,ax
              mov di,frame.row        ;place row in DI and multiply by 160
              mov ax,di
              mov cl,7
              shl di,cl
              mov cl,5
              shl ax,cl
              add di,ax
              mov ax,frame.col        ;multiply col by 2
              shl ax,1
              add di,ax               ;add (col * 2) to DI

              mov ah,byte ptr frame.att
              cld

c03:          cmp byte ptr [si],0
              je c04
              lodsb                   ;load a single character from source
              stosw                   ;write a char and attribute to dest
              jmp c03

c04:          pop si
              pop di
              mov sp,bp
              pop bp
              ret
_printv       endp

_printw       proc near       ;void printw (buf,ulr_s,ulc_s,lrr_s,ulr_d,ulc_d);
                              ;char *buf;
                              ;int ulr_s,ulc_s,lrr_s,lrc_s,ulr_d,ulc_d;
bframe        struc
bbptr         dw  ?
bret_ad       dw  ?
buf_ad        dw  ?        ;address of source buffer
ulr_s         dw  ?        ;upper-left row source
ulc_s         dw  ?        ;upper-left column source
lrr_s         dw  ?        ;lower-right row source
lrc_s         dw  ?        ;lower-right column source
ulr_d         dw  ?        ;upper-left row destination
ulc_d         dw  ?        ;upper-left column destinayion
bframe        ends

              push bp
              mov bp,sp
              push di
              push si

              mov si,frame.buf_ad
              mov ax,frame.ulr_s
              mov bx,ax
              mov cl,7
              shl ax,cl
              mov cl,5
              shl bx,cl
              add bx,ax

              mov ax,frame.ulc_s
              shl ax,1
              add bx,ax

              add si,bx

              xor ax,ax
              mov es,ax
              cmp byte ptr es:[449h],7
              je d01
              mov ax,0b800h           ;color buffer
              jmp d02
d01:          mov ax,0b000h           ;monochrome buffer
d02:          mov es,ax

              mov di,frame.ulr_d
              mov ax,di
              mov cl,7
              shl di,cl
              mov cl,5
              add di,ax

              mov ax,frame.ulc_d
              shl ax,1
              add di,ax

              mov bl,byte ptr frame.lrr_s
              sub bl,byte ptr frame.ulr_s
              inc bl

              mov dx,frame.lrc_s
              sub dx,frame.ulc_s
              inc dx
              shl dx,1
              mov ax,160
              sub ax,dx

              cld
d03:          mov cx,dx
              rep movsb
              add si,ax
              add di,ax
              dec bl
              jnz d03

              pop si
              pop di
              mov sp,bp
              pop bp
              ret
_printw       endp

_getstr       proc near     ;coid getstr(buf,att,row,col,num);
                            ;char *buf;
                            ;int att,row,col,num;
cframe        struc
cbptr         dw  ?
cret_ad       dw  ?
cbuf_ad       dw  ?         ;buffer address
catt          dw  ?         ;display attribute
crow          dw  ?         ;starting row
ccol          dw  ?         ;starting column
num           dw  ?         ;nnumber of byte in receving buffer
cframe        ends


              push bp
              mov bp,sp

              push si
              push di
              push es

              xor ax,ax
              mov es,ax
              cmp byte ptr es:[449h],7
              je e01
              mov ax,0b800h
              jmp e02
e01:          mov ax,0b000h
e02:          mov es,ax
              mov bx,frame.cbuf_ad
              mov si,bx
              add si,frame.num
              dec si
              mov ch,byte ptr frame.catt
              mov dh,byte ptr frame.crow
              mov dl,byte ptr frame.ccol
              cursor

e03:          mov ah,0
              int 16h

              cmp ah,1
              jne e04
              mov ax,1
              jmp e10

e04:          cmp al,13
              jne e05
              jmp e09
e05:          cmp al,8
              jne e06
              cmp dl,byte ptr frame.ccol
              je e03
              dec dl
              cursor
              mov cl,32
              putchar
              dec bx
              jmp e03
e06:          cmp bx,si
              jne e07
              jmp e03
e07:          cmp al,0
              jne e08
              jmp e03
e08:          mov cl,al
              putchar
              inc dl
              cursor
              mov [bx],al
              inc bx
              jmp e03

e09:          mov ax,0
e10:          mov byte ptr [bx],0
              pop es
              pop di
              pop si
              pop bp
              ret
_getstr       endp

_box          proc near  ;display box on screen

dframe        struc
dbptr         dw  ?
dret_ad       dw  ?
dulr          dw  ?        ;upper-left row source
dulc          dw  ?        ;upper-left column source
dlrr          dw  ?        ;lower-right row source
dlrc          dw  ?        ;lower-right column source
dframe        ends

              push bp
              mov bp,sp

              xor ax,ax
              mov es,ax
              cmp byte ptr es:[449h],7 ;test video mode
              je f01
              mov ax,0b800h           ;color buffer
              jmp f02
f01:          mov ax,0b000h           ;monochrome buffer
f02:          mov es,ax

              mov ah,byte ptr frame.dulr
              mov al,byte ptr frame.dlrr

              mov ch,INITATTR
              mov cl,201
              mov dh,ah
              mov dl,byte ptr frame.dulc
              putchar

              mov cl,200
              mov dh,al
              putchar

              mov bl,byte ptr frame.dlrc
              sub bl,dl
              dec bl

              mov cl,205
f03:          inc dl
              mov dh,ah
              putchar
              mov dh,al
              putchar
              dec bl
              je f04
              jmp f03

f04:          mov cl,188
              inc dl
              putchar

              mov cl,187
              mov dh,ah
              putchar

              mov bl,al
              sub bl,ah
              dec bl
              mov ah,byte ptr frame.dulc
              mov al,dl

              mov cl,186
f05:          inc dh
              mov dl,ah
              putchar
              mov dl,al
              putchar
              dec bl
              je f06
              jmp f05

f06:          pop bp
              ret
_box          endp

_cls          proc near    ;clear box on screen

eframe        struc
ebptr         dw  ?
eret_ad       dw  ?
eulr          dw  ?        ;upper-left row source
eulc          dw  ?        ;upper-left column source
elrr          dw  ?        ;lower-right row source
elrc          dw  ?        ;lower-right column source
eframe        ends

              push bp
              mov bp,sp

              mov ah,6
              xor al,al
              mov ch,byte ptr frame.eulr
              mov cl,byte ptr frame.eulc
              mov dh,byte ptr frame.elrr
              mov dl,byte ptr frame.elrc
              mov bh,0fh
              int 10h

              mov sp,bp
              pop bp
              ret

_cls          endp

_hidecur      proc near

              mov ah,3
              mov bh,0
              int 10h
              mov oldcur,cx

              mov ah,1
              mov ch,32
              mov cl,0
              int 10h
              ret

_hidecur      endp

_showcur      proc near

              mov ah,1
              mov cx,oldcur
              int 10h

              ret

_showcur      endp

_savecur      proc near

              mov ah,3
              mov bh,0
              int 10h
              mov curpos,dx
              ret

_savecur      endp

_restcur      proc near

              mov ah,2
              mov bh,0
              mov dx,curpos
              int 10h
              ret

_restcur      endp

_text         ends

              end